package com.ly.heart.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.core.domain.AjaxResult;
import com.ly.common.core.domain.entity.SysDept;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.mapper.KingBaseCostCenterMapper;
import com.ly.heart.service.IKingBaseCostCenterService;
import com.ly.heart.util.CurrentUserUtil;
import com.ly.system.service.ISysDeptService;
import org.springframework.stereotype.Service;
import com.ly.heart.domain.KingBaseCostCenterEntity;

import javax.annotation.Resource;

/**
 * 金蝶-财务模块-成本中心Service业务层处理
 *
 * @author wutao
 * @date 2024-06-22
 */
@Service
public class KingBaseCostCenterServiceImpl extends ServiceImpl<KingBaseCostCenterMapper, KingBaseCostCenterEntity> implements IKingBaseCostCenterService {

    @Resource
    private KingBaseCostCenterMapper kingBaseCostCenterEntityMapper;
    @Resource
    private ISysDeptService iSysDeptService;

    /**
     * 查询金蝶-财务模块-成本中心
     *
     * @param id 金蝶-财务模块-成本中心ID
     * @return 金蝶-财务模块-成本中心
     */
    @Override
    public KingBaseCostCenterEntity selectKingBaseCostCenterEntityById ( String id ) {
        return kingBaseCostCenterEntityMapper.selectKingBaseCostCenterEntityById ( id );
    }

    /**
     * 查询金蝶-财务模块-成本中心列表
     *
     * @param kingBaseCostCenterEntity 金蝶-财务模块-成本中心
     * @return 金蝶-财务模块-成本中心
     */
    @Override
    public Page selectKingBaseCostCenterEntityList ( KingBaseCostCenterEntity kingBaseCostCenterEntity ) {
        Page page = this.page ( new Page<> ( kingBaseCostCenterEntity.getPage (), kingBaseCostCenterEntity.getLimit () ), new LambdaQueryWrapper<KingBaseCostCenterEntity> ().eq ( KingBaseCostCenterEntity :: getIsUsed, ConstUtil.IS_USED_YES ) );
        List<KingBaseCostCenterEntity> records = page.getRecords ();
        records.forEach ( cost -> {
            SysDept company = iSysDeptService.getById ( cost.getDeptId () );
            if ( company != null ) {
                cost.setDeptName ( company.getDeptName () );
            }
            SysDept dept = iSysDeptService.getById ( cost.getRelation () );
            if ( dept != null ) {
                cost.setRelation ( dept.getDeptName () );
            }
            String deptAttr = cost.getDeptAttr ();
            //0：采购；1：销售；2：研发；3：管理；4：基本生产；5：辅助生产；6：委外生产
            String attr = "0".equals ( deptAttr ) ? "采购" :
                    ( "1".equals ( deptAttr ) ? "销售" :
                            ( "2".equals ( deptAttr ) ? "研发" :
                                    ( "3".equals ( deptAttr ) ? "管理" :
                                            ( "4".equals ( deptAttr ) ? "基本生产" :
                                                    ( "5".equals ( deptAttr ) ? "辅助生产" : "委外生产" ) ) ) ) );
            cost.setDeptAttr ( attr );
        } );
        page.setRecords ( records );
        return page;
    }

    /**
     * 新增金蝶-财务模块-成本中心
     *
     * @param kingBaseCostCenterEntity 金蝶-财务模块-成本中心
     * @return 结果
     */
    @Override
    public int insertKingBaseCostCenterEntity ( KingBaseCostCenterEntity kingBaseCostCenterEntity ) {
        kingBaseCostCenterEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        int count = this.count ();
        kingBaseCostCenterEntity.setCostCode ( "CBZX".concat ( String.format ( "%08d", count ) ) );
        kingBaseCostCenterEntity.setDeptId ( CurrentUserUtil.getCompanyId () );
        kingBaseCostCenterEntity.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingBaseCostCenterEntity.setCreateTime ( DateUtils.getNowDate () );
        return kingBaseCostCenterEntityMapper.insertKingBaseCostCenterEntity ( kingBaseCostCenterEntity );
    }

    /**
     * 修改金蝶-财务模块-成本中心
     *
     * @param kingBaseCostCenterEntity 金蝶-财务模块-成本中心
     * @return 结果
     */
    @Override
    public int updateKingBaseCostCenterEntity ( KingBaseCostCenterEntity kingBaseCostCenterEntity ) {
        kingBaseCostCenterEntity.setUpdateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingBaseCostCenterEntity.setUpdateTime ( DateUtils.getNowDate () );
        return kingBaseCostCenterEntityMapper.updateKingBaseCostCenterEntity ( kingBaseCostCenterEntity );
    }

    /**
     * 批量删除金蝶-财务模块-成本中心
     *
     * @param ids 需要删除的金蝶-财务模块-成本中心ID
     * @return 结果
     */
    @Override
    public AjaxResult deleteKingBaseCostCenterEntityByIds ( String[] ids ) {
        List<KingBaseCostCenterEntity> list = this.list ( new LambdaQueryWrapper<KingBaseCostCenterEntity> ().in ( KingBaseCostCenterEntity :: getId, ids ) );
        List<String> idList = list.stream ().filter ( cost -> cost.getIsDel () == 1 ).map ( KingBaseCostCenterEntity :: getId ).collect ( Collectors.toList () );
        StringBuilder stringBuilder = new StringBuilder ( "【" );
        list.forEach ( cost -> {
            if ( cost.getIsDel () == 2 ) {
                stringBuilder.append ( cost.getTitle () );
            }
        } );
        stringBuilder.append ( "】" );
        boolean update = this.update ( new LambdaUpdateWrapper<KingBaseCostCenterEntity> ().set ( KingBaseCostCenterEntity :: getIsUsed, ConstUtil.IS_USED_NO ).
                set ( KingBaseCostCenterEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingBaseCostCenterEntity :: getUpdateTime, DateUtils.getNowDate () ).
                in ( KingBaseCostCenterEntity :: getId, idList ) );
        if ( ! update ) {
            throw new CustomException ( "删除成本失败！" );
        }
        String str = "删除成本成功";
        if ( list.size () > 0 ) {
            str.concat ( "，其中" ).concat ( stringBuilder.toString () ).concat ( "被引用，不可删除" );
        }
        return AjaxResult.success ( str );
    }

    /**
     * 删除金蝶-财务模块-成本中心信息
     *
     * @param id 金蝶-财务模块-成本中心ID
     * @return 结果
     */
    @Override
    public AjaxResult disabled ( String id ) {
        KingBaseCostCenterEntity costCenterEntity = this.getById ( id );
        if ( costCenterEntity == null ) {
            throw new CustomException ( "无此成本中心！" );
        }
        boolean update = this.update ( new LambdaUpdateWrapper<KingBaseCostCenterEntity> ().
                set ( KingBaseCostCenterEntity :: getIsDisabled, costCenterEntity.getIsDisabled () == 1 ? 2 : 1 ).
                set ( KingBaseCostCenterEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingBaseCostCenterEntity :: getUpdateTime, DateUtils.getNowDate () ).
                in ( KingBaseCostCenterEntity :: getId, id ) );
        if ( ! update ) {
            throw new CustomException ( "删除成本失败！" );
        }
        return AjaxResult.success ();
    }

}
